package com.xing.algorithm.string;

public class Strstr {
    public static int strStr(String haystack, String needle) {
        if(haystack == null || needle == null) {
            return -1;
        }

        if(needle.trim().equals("")) {
            return 0;
        }

        if(needle.length() > haystack.length()) {
            return -1;
        }

        int j = 0;
        int ii = 0;
        for(int i = 0; i < haystack.length(); i ++) {
            ii = i;
            while(j < needle.length() && ii < haystack.length() && haystack.charAt(ii) == needle.charAt(j)){
                j++;
                ii++;
            }

            if(j == needle.length() ) {
                return i;
            }

            if(j != 0) {
                j = 0;
            }
        }

        return -1;
    }

    public static void main(String[] args){
        System.out.println(strStr("mississippi"
                ,"issip"));
    }
}
